package com.ruoyi.mng.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mng.service.IHzBusiService;
import com.ruoyi.mng.service.IHzMerMerchantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mng.mapper.HzMerBusiRMapper;
import com.ruoyi.mng.domain.HzMerBusiR;
import com.ruoyi.mng.service.IHzMerBusiRService;
import com.ruoyi.common.core.text.Convert;

import javax.annotation.Resource;

/**
 * 商户业务关系Service业务层处理
 *
 * @author chenqiang
 * @date 2023-04-27
 */
@Service
public class HzMerBusiRServiceImpl implements IHzMerBusiRService {
    @Autowired
    private HzMerBusiRMapper hzMerBusiRMapper;

    @Resource
    private IHzBusiService hzBusiService;
    @Resource
    private IHzMerMerchantService hzMerMerchantService;

    /**
     * 查询商户业务关系
     *
     * @param rId 商户业务关系主键
     * @return 商户业务关系
     */
    @Override
    public HzMerBusiR selectHzMerBusiRByRId(Long rId) {
        HzMerBusiR hzMerBusiR = hzMerBusiRMapper.selectHzMerBusiRByRId(rId);
        hzMerBusiR.setExtend_merchantName(hzMerMerchantService.selectHzMerMerchantByMerchantId(hzMerBusiR.getMerchantId()).getMerchantShortName());
        hzMerBusiR.setExtend_busi(hzBusiService.selectHzBusiByBusiId(hzMerBusiR.getBusiId()).getBusiName());
        if (hzMerBusiR.getFee1() != null) {
            hzMerBusiR.setExtend_fee1(hzMerBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString());
        }
        if (hzMerBusiR.getFee2() != null) {
            hzMerBusiR.setExtend_fee2(hzMerBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString());
        }
        return hzMerBusiR;
    }

    /**
     * 查询商户业务关系列表
     *
     * @param hzMerBusiR 商户业务关系
     * @return 商户业务关系
     */
    @Override
    public List<HzMerBusiR> selectHzMerBusiRList(HzMerBusiR hzMerBusiR) {
        List<HzMerBusiR> merBusiRList = hzMerBusiRMapper.selectHzMerBusiRList(hzMerBusiR);
        merBusiRList.forEach(this::setExtendHzMerBusiR);
        return merBusiRList;
    }

    /**
     * description :: 给HzMerBusiR设置扩展信息
     */
    private void setExtendHzMerBusiR(HzMerBusiR hzMerBusiR) {
        hzMerBusiR.setExtend_busi(hzBusiService.selectHzBusiByBusiId(hzMerBusiR.getBusiId()).getBusiName());
        String extend_fee = "-";
        if (hzMerBusiR.getFeeType() == 1) {
            //单笔
            extend_fee = hzMerBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString();
        } else if (hzMerBusiR.getFeeType() == 2) {
            //费率
            extend_fee = hzMerBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString() + "‰";
        } else if (hzMerBusiR.getFeeType() == 3) {
            //单笔+费率
            extend_fee = hzMerBusiR.getFee1().divide(new BigDecimal(100)).setScale(2, RoundingMode.HALF_UP).toPlainString()
                    + " + " + hzMerBusiR.getFee2().multiply(new BigDecimal(1000)).stripTrailingZeros().toPlainString() + "‰";
        }
        hzMerBusiR.setExtend_fee(extend_fee);
        hzMerBusiR.setExtend_merchantName(hzMerMerchantService.selectHzMerMerchantByMerchantId(hzMerBusiR.getMerchantId()).getMerchantShortName());
    }

    /**
     * 新增商户业务关系
     *
     * @param hzMerBusiR 商户业务关系
     * @return 结果
     */
    @Override
    public int insertHzMerBusiR(HzMerBusiR hzMerBusiR) {
        if (hzMerBusiR.getFee1() != null) {
            hzMerBusiR.setFee1(hzMerBusiR.getFee1().multiply(new BigDecimal(100)));
        }
        if (hzMerBusiR.getFee2() != null) {
            hzMerBusiR.setFee2(hzMerBusiR.getFee2().divide(new BigDecimal(1000)));
        }
        hzMerBusiR.setCreateTime(DateUtils.getNowDate());
        hzMerBusiR.setUpdateTime(DateUtils.getNowDate());
        return hzMerBusiRMapper.insertHzMerBusiR(hzMerBusiR);
    }

    /**
     * 修改商户业务关系
     *
     * @param hzMerBusiR 商户业务关系
     * @return 结果
     */
    @Override
    public int updateHzMerBusiR(HzMerBusiR hzMerBusiR) {
        if (hzMerBusiR.getFee1() != null) {
            hzMerBusiR.setFee1(hzMerBusiR.getFee1().multiply(new BigDecimal(100)));
        }
        if (hzMerBusiR.getFee2() != null) {
            hzMerBusiR.setFee2(hzMerBusiR.getFee2().divide(new BigDecimal(1000)));
        }
        hzMerBusiR.setUpdateTime(DateUtils.getNowDate());
        return hzMerBusiRMapper.updateHzMerBusiR(hzMerBusiR);
    }

    /**
     * 批量删除商户业务关系
     *
     * @param rIds 需要删除的商户业务关系主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBusiRByRIds(String rIds) {
        return hzMerBusiRMapper.deleteHzMerBusiRByRIds(Convert.toStrArray(rIds));
    }

    /**
     * 删除商户业务关系信息
     *
     * @param rId 商户业务关系主键
     * @return 结果
     */
    @Override
    public int deleteHzMerBusiRByRId(Long rId) {
        return hzMerBusiRMapper.deleteHzMerBusiRByRId(rId);
    }
}
